package org.scohen.juploadr.uploadapi.zooomr.photosets;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.scohen.juploadr.app.ImageAttributes;
import org.scohen.juploadr.app.PhotoSet;

public class ZooomrPhotoSet implements Comparable<PhotoSet>, PhotoSet {

    private String id;
    private String secret;
    private String server;
    private int numberOfPhotos;
    private String title;
    private String description;
    private ImageAttributes primaryPhoto;
    private String primaryPhotoId;
    private boolean New = false;
    private List<ImageAttributes> photos = new ArrayList<ImageAttributes>();
    private String url;

    /**
     * @return Returns the description.
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description
     *            The description to set.
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return Returns the id.
     */
    public String getId() {
        return id;
    }

    /**
     * @param id
     *            The id to set.
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return Returns the numberOfPhotos.
     */
    public int getNumberOfPhotos() {
        return numberOfPhotos;
    }

    /**
     * @param numberOfPhotos
     *            The numberOfPhotos to set.
     */
    public void setNumberOfPhotos(int numberOfPhotos) {
        this.numberOfPhotos = numberOfPhotos;
    }

    /**
     * @return Returns the secret.
     */
    public String getSecret() {
        return secret;
    }

    /**
     * @param secret
     *            The secret to set.
     */
    public void setSecret(String secret) {
        this.secret = secret;
    }

    /**
     * @return Returns the server.
     */
    public String getServer() {
        return server;
    }

    /**
     * @param server
     *            The server to set.
     */
    public void setServer(String server) {
        this.server = server;
    }

    /**
     * @return Returns the title.
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title
     *            The title to set.
     */
    public void setTitle(String title) {
        this.title = title;
    }

    public class TitleComparator implements Comparator<PhotoSet> {

        public int compare(PhotoSet p1, PhotoSet p2) {
            return p1.getTitle().compareTo(p2.getTitle());
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object o) {
        if (o instanceof ZooomrPhotoSet) {
            ZooomrPhotoSet p = (ZooomrPhotoSet) o;
            if (id != null) {
                return id.equals(p.getId());
            } else {
                return title.equals(p.getTitle());
            }
        }
        return false;
    }

    /**
     * @return Returns the new.
     */
    public boolean isNew() {
        return New;
    }

    /**
     * @param new1
     *            The new to set.
     */
    public void setNew(boolean new1) {
        New = new1;
    }

    /**
     * @return Returns the primaryPhoto.
     */
    public ImageAttributes getPrimaryPhoto() {
        return primaryPhoto;
    }

    /**
     * @param primaryPhoto
     *            The primaryPhoto to set.
     */
    public void setPrimaryPhoto(ImageAttributes primaryPhoto) {
        this.primaryPhoto = primaryPhoto;
    }

    /**
     * @return Returns the primaryPhotoId.
     */
    public String getPrimaryPhotoId() {
        if (primaryPhoto != null && primaryPhoto.getPhotoId() != null) {
            return primaryPhoto.getPhotoId();

        }
        return primaryPhotoId;
    }

    /**
     * @param primaryPhotoId
     *            The primaryPhotoId to set.
     */
    public void setPrimaryPhotoId(String primaryPhotoId) {
        this.primaryPhotoId = primaryPhotoId;
    }

    public List getPhotos() {
        return photos;
    }

    public void addPhoto(ImageAttributes atts) {
        if (!photos.contains(atts)) {
            atts.addPhotoSet(this);
            photos.add(atts);
        }
    }

    public void removePhoto(ImageAttributes atts) {
        if (photos.contains(atts)) {
            photos.remove(atts);
            atts.getPhotoSets().remove(this);
        }
    }

    public boolean hasPhoto(ImageAttributes photo) {
        return photos.contains(photo);
    }

    /**
     * @return Returns the url.
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url
     *            The url to set.
     */
    public void setUrl(String url) {
        this.url = url;
    }

    public int compareTo(PhotoSet p1) {
        return title.toLowerCase().compareTo(p1.getTitle().toLowerCase());
    }

}
